package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.util.*;
import java.text.*;
import java.io.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/**
 * Action: Sent by USR to register a new user with the OSS.
 * @author S.N.Fraser
 */
public class REGISTER_USERImpl extends TransactionImpl {

    /** The path used to locate the User in the OSS database. */
    protected Path userPath;
 
    /** Create a REGISTER_USERImpl for the supplied request.*/
    public REGISTER_USERImpl(REGISTER_USER request) {
	super(request);
	this.userPath = request.getUserPath();
    }
    
    /**
     * Registers the user specified in the path.
     * @return unique registration id.
     */
    public TRANSACTION_DONE exec() { 
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 'T' HH:mm:ss");
	myLog = LogManager.getLogger("PEST");

	Transaction t = null;
	try {
	    t = Transaction.current();
	    
	    User user = OSS_Utilities.fetchUser(userPath);
	    
	    // Check it exists.
	    if (user == null) {
		logger.log(1, CLASS, id, "exec", 
			   "User: "+userPath.toString()+" Not in database.");
		return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
				     "Could not find User: "+userPath.toString());
	    } 

	    // Generate a long random number ###
	    long time = System.currentTimeMillis();
	   
	    myLog.log(1, CLASS, id, "exec", 
		      "PEST: User Registration requested"+
		      "\n\tTAG:    "+userPath.getTagByName()+
		      "\n\tUser:   "+userPath.getUserByName()+
		      "\n\tAt:     "+sdf.format(new Date(time))+
		      "\n\tReg ID: "+Long.toHexString(time).toUpperCase());

	    REGISTER_USER_DONE done = new  REGISTER_USER_DONE(id);
	    done.setRegistrationCode(time);
	    done.setSuccessful(true);
	    return done;
	    
	} finally {
	    t.commit();
	}
    }
   
}

/** $Log: REGISTER_USERImpl.java,v $
/** Revision 1.1  2006/11/27 08:45:00  snf
/** Initial revision
/**
/** Revision 1.1  2001/02/23 18:51:14  snf
/** Initial revision
/** */
